﻿using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Yc.Commons;

namespace Rail.Start.Viewer
{
    public partial class FormLogin : XtraForm
    {
        string m_UUID = string.Empty;
        public FormLogin()
        {
            InitializeComponent();
            this.lblTitle.Text = "登录";
            this.BackgroundImage = System.Drawing.Image.FromFile(Commons.Paths.ApplicationResourcesDir + "/loginBg.jpg" );


            this.m_HeaderButtonSetting.Click += HeaderButtonSetting_Click;
            this.m_BtnCancel.Click += FormLogin_ButtonCancelClick;
            this.m_BtnOK.Click += FLogin_ButtonLoginClick;

            this.m_PictureBoxTesting.Click += PictureBoxTesting_Click;
        }

        private void PictureBoxTesting_Click(object sender, EventArgs e)
        {
            GetTestingImg();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            GetTestingImg();
        }
        private void HeaderButtonSetting_Click(object sender, System.EventArgs e)
        {
            FormEditHost feh = new FormEditHost();
            feh.ShowDialog();
        }
        private void FormLogin_ButtonCancelClick(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
        private void FLogin_ButtonLoginClick(object sender, System.EventArgs e)
        {

            var uname = this.m_TextBoxUserName.Text;
            var upw = this.m_TextBoxPassword.Text;
            var code = this.m_TextBoxTestingCode.Text;
            var uuid = this.m_UUID;

            //string publickey = @"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";

            //RSACryptoService rcs = new RSACryptoService(string.Empty,publickey);

            JObject jo = new JObject
            {
                { "username", uname },
                { "password", upw },
                { "code", code },
                { "uuid", uuid }
            };
            Console.WriteLine(jo.ToString());
            var httper = new Httper();
            httper.Post("login",jo.ToString(), (p) =>
            {
                Console.WriteLine(p.ToString());
                //UserName就是封装了界面里用户名输入框的值
                //Password就是封装了界面里密码输入框的值
                JObject jop = (JObject)JsonConvert.DeserializeObject(p.ToString());
                var resultCode = jop["code"];
                if(null == jop)
                {
                    Rail.Commons.LogHelper.Error("服务器错误");
                }
                else if (resultCode.ToString() == "200")
                {
                    httper.SetToken(jop["token"].ToString());
                    Close();
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    GetTestingImg();
                    Rail.Commons.LogHelper.Error(jop["msg"].ToString());
                }

            }, Httper.ResultTypesEnum.TJson);
        }

        private void GetTestingImg()
        {
            var httper = new Httper();
            httper.Get("captchaImage", (p) =>
            {
                JObject jo = (JObject)JsonConvert.DeserializeObject(p.ToString());
                var imgObj = jo["img"];
                Image img = ToImage(imgObj.ToString());
                m_PictureBoxTesting.BackgroundImage = img;
                m_PictureBoxTesting.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;

                m_UUID = jo["uuid"].ToString();
            }, Httper.ResultTypesEnum.TUnKnow);
        }
        private Image ToImage(string imgString)
        {
            byte[] bytes = Convert.FromBase64String(imgString);
            MemoryStream memStream = new MemoryStream(bytes);
            Image img = new System.Drawing.Bitmap(memStream);
            return img;
        }
    }
}
